package com.aliang.shopping.security;

import cn.hutool.core.util.ObjectUtil;
import com.aliang.shopping.common.ResultCodeEnum;
import com.aliang.shopping.exception.ServiceException;
import com.aliang.shopping.model.enums.UserTypeEnum;
import com.aliang.shopping.security.auth.UserHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AuthAspect {


    @Around("@annotation(auth)")
    public Object doAuth(ProceedingJoinPoint joinPoint, Auth auth) throws Throwable{
        //获取需要的信息
        UserTypeEnum role = auth.role();
        Integer userRole = UserHolder.getUserRole();
        //判断角色是否一致
        if (!ObjectUtil.equal(userRole, role.getCode())) {
            throw new ServiceException(ResultCodeEnum.NOT_AUTHORIZED);
        }
        return joinPoint.proceed();
    }
}
